package com.fenghongzhang.day004;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.appcompat.app.AppCompatActivity;

public class Main3Activity extends AppCompatActivity {

    private CheckBox qiu;
    private CheckBox game;
    private CheckBox all;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        //1初始化
          qiu = findViewById(R.id.qiu);
          game = findViewById(R.id.game);
          all = findViewById(R.id.all);

          //选项的功能
        all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //全选被选中了
                if(all.isChecked()){
                    qiu.setChecked(true);
                    game.setChecked(true);
                }else{
                    qiu.setChecked(false);
                    game.setChecked(false);
                }
            }
        });

        MyListener myListener = new MyListener();
        qiu.setOnCheckedChangeListener(myListener);
        game.setOnCheckedChangeListener(myListener);

    }

    class MyListener implements CompoundButton.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (qiu.isChecked() && game.isChecked()){
                all.setChecked(true);
            }else{
                all.setChecked(false);
            }
        }
    }
}
